﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;

namespace AdminUI.Pages.OrderManage
{
    /// <summary>
    /// 商城订单：订单查询
    /// </summary>
    public partial class OrderModel : BaseModel
    {
        public void OnGet()
        {
            var req = new PagingRequest
            {
                PageIndex = 1,
                PageSize = 20
            };
            var result = OrderBO.Instance.Query(req, OnlineTID);
            ViewBag.DataSource = result.Data;
            ViewBag.Total = result.Total;
            ViewBag.PageSize = result.PageSize;
        }

        public IActionResult OnPostGrid_DoAction(QueryData<long> data, string condition, string keyword, int status)
        {
            var grid = UIHelper.Grid("Grid1");
            var req = new PagingRequest
            {
                PageIndex = data.PageIndex + 1, //注意：数据表格组件分页索引从0开始
                PageSize = data.ChangedPageSize,
                SortExp = data.SortExp,
            };
            if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(condition))
            {
                req.FilterItems.Add(new FilterItem { Field = condition, Value = keyword });
            }

            if (data.IsChangedPageSize)
            {
                grid.PageSize(data.ChangedPageSize);
                grid.PageIndex(0); //改变PageSize大小后重置分页索引，表格控件页索引从0开始
                req.PageIndex = 1;
            }

            //if (status < 999) status = 0;
            switch (data.ActionType)
            {
                //case "delete":
                //    OrderBO.Instance.Delete(data.SelectedID);
                //    LogAction(ActionType.删除, $"删除商城订单：{data.SelectedID}");
                //    break;
                //case "cancel":
                //    OrderBO.Instance.Cancel(data.SelectedID);
                //    LogAction(ActionType.取消, $"取消商城订单：{data.SelectedID}");
                //    break;
                default:
                    break;
            }

            var result = OrderBO.Instance.Query(req, OnlineTID, status);
            grid.RecordCount(result.Total);
            grid.DataSource(result.Data, data.Fields);
            return UIHelper.Result();
        }
    }
}